﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Xps.Packaging;
using System.IO;

namespace XPSReaderWPF
{
    class XPSConversion
    {
        string _fileName;
        #region FileName
        /// <summary>
        /// Gets or sets the name of the file.
        /// </summary>
        /// <value>The name of the file.</value>
        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }
        #endregion

        int _pageCount;
        #region PageCount
        /// <summary>
        /// Gets or sets the page count.
        /// </summary>
        /// <value>The page count.</value>
        public int PageCount
        {
            get { return _pageCount; }
            set { _pageCount = value; }
        }
        #endregion

        double _pageHeight;
        #region PageHeight
        public double PageHeight
        {
            get { return _pageHeight; }
            set { _pageHeight = value; }
        }
        #endregion

        double _pageWidth;
        #region PageWidth
        public double PageWidth
        {
            get { return _pageWidth; }
            set { _pageWidth = value; }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="XPSConvertion"/> class.
        /// </summary>
        /// <param name="filename">The filename.</param>
        public XPSConversion(string filename)
        {
            FileName = filename;
            PageCount = 0;
        }


        /// <summary>
        /// Saves the XPS page to bitmap.
        /// </summary>
        /// <param name="xpsFileName">Name of the XPS file.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public int SaveXpsPageToBitmap(string xpsFileName, string name)
        {
            // XPS Document
            XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
            FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();

            // The number of pages
            PageCount = docSeq.References[0].GetDocument(false).Pages.Count;

            DocumentPage sizePage = docSeq.DocumentPaginator.GetPage(0);
            PageHeight = sizePage.Size.Height;
            PageWidth = sizePage.Size.Width;

            // Convert a XPS page to a PNG file
            for (int pageNum = 0; pageNum < PageCount; pageNum++)
            {
                DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageNum);
                BitmapImage bitmap = new BitmapImage();
                RenderTargetBitmap renderTarget =
                    new RenderTargetBitmap((int)docPage.Size.Width,
                                            (int)docPage.Size.Height,
                                            96,
                                            96,
                                            System.Windows.Media.PixelFormats.Pbgra32);
                renderTarget.Render(docPage.Visual);

                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(renderTarget));

                FileStream pageOutStream = new FileStream(name + ".Page" + pageNum + ".png", FileMode.Create, FileAccess.Write);
                encoder.Save(pageOutStream);
                pageOutStream.Close();
            }

            return PageCount;
        }
    }
}
